home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / fstab.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  7.8 KB  |  194 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import os
  5. import re
  6. import tempfile
  7.  
  8. class Line(object):
  9.     '''A line in an /etc/fstab line.
  10.  
  11.     Lines may or may not have a filesystem specification in them. The
  12.     has_filesystem method tells the user whether they do or not; if they
  13.     do, the attributes device, directory, fstype, options, dump, and
  14.     fsck contain the values of the corresponding fields, as instances of
  15.     the sub-classes of the LinePart class. For non-filesystem lines,
  16.     the attributes have the None value.
  17.     
  18.     Lines may or may not be syntactically correct. If they are not,
  19.     they are treated as as non-filesystem lines.
  20.     
  21.     '''
  22.     attrs = ('ws1', 'device', 'ws2', 'directory', 'ws3', 'fstype')
  23.     attrs += ('ws4', 'options', 'ws5', 'dump', 'ws6', 'fsck', 'ws7')
  24.     
  25.     def __init__(self, raw):
  26.         self.dict = { }
  27.         self.raw = raw
  28.  
  29.     
  30.     def __getattr__(self, name):
  31.         if name in self.dict:
  32.             return self.dict[name]
  33.         raise AttributeError(name)
  34.  
  35.     
  36.     def __setattr__(self, name, value):
  37.         forbidden = ('dict', 'dump', 'fsck', 'options')
  38.         if name not in forbidden and name in self.dict:
  39.             if self.dict[name] is None:
  40.                 raise Exception('Cannot set attribute %s when line dies not contain filesystem specification' % name)
  41.             self.dict[name] is None
  42.             self.dict[name] = value
  43.         else:
  44.             object.__setattr__(self, name, value)
  45.  
  46.     
  47.     def get_dump(self):
  48.         return int(self.dict['dump'])
  49.  
  50.     
  51.     def set_dump(self, value):
  52.         self.dict['dump'] = str(value)
  53.  
  54.     dump = property(get_dump, set_dump)
  55.     
  56.     def get_fsck(self):
  57.         return int(self.dict['fsck'])
  58.  
  59.     
  60.     def set_fsck(self, value):
  61.         self.dict['fsck'] = str(value)
  62.  
  63.     fsck = property(get_fsck, set_fsck)
  64.     
  65.     def get_options(self):
  66.         return self.dict['options'].split(',')
  67.  
  68.     
  69.     def set_options(self, list):
  70.         self.dict['options'] = ','.join(list)
  71.  
  72.     options = property(get_options, set_options)
  73.     
  74.     def set_raw(self, raw):
  75.         pat = '^(?P<ws1>\\s*)'
  76.         pat += '(?P<device>\\S+)'
  77.         pat += '(?P<ws2>\\s+)'
  78.         pat += '(?P<directory>\\S+)'
  79.         pat += '(?P<ws3>\\s+)'
  80.         pat += '(?P<fstype>\\S+)'
  81.         pat += '(?P<ws4>\\s+)'
  82.         pat += '(?P<options>\\S+)'
  83.         pat += '(?P<ws5>\\s+)'
  84.         pat += '(?P<dump>\\d+)'
  85.         pat += '(?P<ws6>\\s+)'
  86.         pat += '(?P<fsck>\\d+)'
  87.         pat += '(?P<ws7>\\s*)$'
  88.         match = re.match(pat, raw)
  89.         if match:
  90.             (self.dict.update,)((lambda .0: for attr in .0:
  91. (attr, match.group(attr)))(self.attrs))
  92.         else:
  93.             self.dict.update((lambda .0: for attr in .0:
  94. (attr, None))(self.attrs))
  95.         self.dict['raw'] = raw
  96.  
  97.     
  98.     def get_raw(self):
  99.         if self.has_filesystem():
  100.             return (''.join,)((lambda .0: for attr in .0:
  101. self.dict[attr])(self.attrs))
  102.         return self.dict['raw']
  103.  
  104.     raw = property(get_raw, set_raw)
  105.     
  106.     def has_filesystem(self):
  107.         '''Does this line have a filesystem specification?'''
  108.         return self.device is not None
  109.  
  110.  
  111.  
  112. class Fstab(object):
  113.     '''An /etc/fstab file.'''
  114.     
  115.     def __init__(self):
  116.         self.lines = []
  117.  
  118.     
  119.     def open_file(self, filespec, mode):
  120.         if type(filespec) in (str, unicode):
  121.             return file(filespec, mode)
  122.         return filespec
  123.  
  124.     
  125.     def close_file(self, f, filespec):
  126.         if type(filespec) in (str, unicode):
  127.             f.close()
  128.         
  129.  
  130.     
  131.     def get_perms(self, filename):
  132.         return os.stat(filename).st_mode
  133.  
  134.     
  135.     def chmod_file(self, filename, mode):
  136.         os.chmod(filename, mode)
  137.  
  138.     
  139.     def link_file(self, oldname, newname):
  140.         if os.path.exists(newname):
  141.             os.remove(newname)
  142.         
  143.         os.link(oldname, newname)
  144.  
  145.     
  146.     def rename_file(self, oldname, newname):
  147.         os.rename(oldname, newname)
  148.  
  149.     
  150.     def read(self, filespec):
  151.         '''Read in a new file.
  152.         
  153.         If filespec is a string, it is used as a filename. Otherwise
  154.         it is used as an open file.
  155.         
  156.         The existing content is replaced.
  157.         
  158.         '''
  159.         f = self.open_file(filespec, 'r')
  160.         lines = []
  161.         for line in f:
  162.             lines.append(Line(line))
  163.         
  164.         self.lines = lines
  165.         self.close_file(filespec, f)
  166.  
  167.     
  168.     def write(self, filespec):
  169.         '''Write out a new file.
  170.         
  171.         If filespec is a string, it is used as a filename. Otherwise
  172.         it is used as an open file.
  173.         
  174.         '''
  175.         if type(filespec) in (str, unicode):
  176.             dirname = os.path.dirname(filespec)
  177.             prefix = os.path.basename(filespec) + '.'
  178.             (fd, tempname) = tempfile.mkstemp(dir = dirname, prefix = prefix)
  179.             os.close(fd)
  180.         else:
  181.             tempname = filespec
  182.         f = self.open_file(tempname, 'w')
  183.         for line in self.lines:
  184.             f.write(line.raw)
  185.         
  186.         self.close_file(filespec, f)
  187.         if type(filespec) in (str, unicode):
  188.             self.chmod_file(tempname, self.get_perms(filespec))
  189.             self.link_file(filespec, filespec + '.bak')
  190.             self.rename_file(tempname, filespec)
  191.         
  192.  
  193.  
  194.